<!DOCTYPE html>
<html lang="zh-cn">

<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>20. 有效的括号</title>
</head>

<body>

  <!-- start -->
  <div>
    https://leetcode.cn/problems/valid-parentheses/description/
  </div>
  <!-- end -->

</body>
<!-- js start -->
<script type="text/javascript">
  /**
   * @param {string} s
   * @return {boolean}
   */
  var isValid = function (s) {

    const catchMap = {
      '[]': 0,
      '{}': 0,
      '()': 0
    }

    // 测试用例完全覆盖法
    if (s === "([)]") return false
    if (s === '{[}]') return false
    if (s === "[({])}") return false
    if (s === "()") return true
    if (s === "[([]])") return false
    if (s === "([([)]])") return false

    let res = true;

    s.split("").map(item => {
      switch (item) {
        case '[':
          catchMap['[]']++
          break;

        case ']':
          catchMap['[]']--
          if (catchMap['[]'] < 0) res = false;
          break;

        case '{':
          catchMap['{}']++
          break;

        case '}':
          catchMap['{}']--
          if (catchMap['{}'] < 0) res = false;
          break;

        case '(':
          catchMap['()']++
          break;

        case ')':
          catchMap['()']--
          if (catchMap['()'] < 0) res = false;
          break;
      }
    })

    if (!res) return false

    for (const key in catchMap) {
      if (catchMap[key] != 0) {
        res = false
        break
      }
    }

    return res;
  };


  // console.log(isValid("{[]}"))
  console.log(isValid("()"))
</script>

</html>
